﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using PPI.Entities.Enumerator;
using PPI.Entities.Settings.Mobile;
using PPI.ServiceLogic.Mobile;

namespace PPI.ServiceLogic.Deploy
{
    /// <summary>
    /// Class that analyses and executes the deployment based on the  database type set on the configuration table
    /// </summary>
    public class DeployBase
    {
        protected EMobileSettingsBase m_SettingsMobile;
        protected LFillMobileTablesBase m_StrategyFillTables;
        private string m_SourceFileName;
        private string m_DestinationFileName;
        private string m_ConnectionKey;

        public DeployBase(EMobileSettingsBase settings, string sourcefileName, string destinationFilename, string connectionKey)
        {
            m_SettingsMobile = settings;
            m_SourceFileName = sourcefileName;
            m_DestinationFileName = destinationFilename;
            m_ConnectionKey = connectionKey;
            CopyMobileDatabase();

        }

        /// <summary>
        /// Copies the origin database to the destination database
        /// </summary>
        private void CopyMobileDatabase()
        {
            File.Copy(m_SourceFileName,m_DestinationFileName,true);
        }

        /// <summary>
        /// Configures the database
        /// </summary>
        public virtual void ConfigMobileDatabase()
        {
            m_SettingsMobile.ConfigurateConfig();
            LMobileSettings.ExecuteSettings(m_SettingsMobile,m_DestinationFileName);
        }
        
        /// <summary>
        /// Fills the  MobileDatabase data obtained from the central database.
        /// </summary>
        public virtual void FillMobileTables()
        {
            switch(m_SettingsMobile.DatabaseType)
            {
                case DatabaseTypes.GPS:
                    m_StrategyFillTables = new LFillMobileTableGPS(m_ConnectionKey,m_DestinationFileName);
                    break;
                case DatabaseTypes.Inspection:
                    m_StrategyFillTables = new LFillMobileTableInspection(m_ConnectionKey, m_DestinationFileName);
                    break;
                case DatabaseTypes.Project:
                    m_StrategyFillTables = new LFillMobileTableProject(m_ConnectionKey, m_DestinationFileName);
                    break;
                case DatabaseTypes.DistributionInspection:
                    LFillMobileTableDistribution FillTableStrategy = null;
                    
                    if (m_SettingsMobile != null )
                        FillTableStrategy = new LFillMobileTableDistribution(m_ConnectionKey, m_DestinationFileName, ((EMobileSettingDistribution)m_SettingsMobile).Utility);
                    else
                        FillTableStrategy = new LFillMobileTableDistribution(m_ConnectionKey, m_DestinationFileName, null);

                    if (m_SettingsMobile != null)
                        FillTableStrategy.VendorProjectIdsToFilter.Add(((EMobileSettingDistribution)m_SettingsMobile).Project.Id);
                    
                    m_StrategyFillTables = FillTableStrategy;
                    break;
                case DatabaseTypes.DistributionGPS:
                    m_StrategyFillTables = new LFillMobileTableDistribution(m_ConnectionKey, m_DestinationFileName, null);
                    break;
            }
                
            m_StrategyFillTables.FillTables();
        }

        /// <summary>
        /// Returns the new created database
        /// </summary>
        public string NewMobileDatabase
        {
            get { return m_DestinationFileName; }
        }
    }
}
